mod sm3;

use std::env;

use sm3::sm3_digest::SM3Digest;
use crate::sm3::error::Error;

fn main() {
    let args :Vec<String> = env::args().collect();
    if  args.len() != 3 {
        print_help();
        return;
    }

    let flag = args[1].trim();
    let arg = args[2].trim();

    let mut sm3 = SM3Digest::new();

    let r_hash = match flag {
        "-f" | "--file" => sm3.hash_file(arg),
        "-i" | "--input"=> sm3.hash_string(arg),
        _=>{
            print_help();
            Err(Error::new(String::from("invalid flags")))
        },
    };

    if let Ok(hash)= r_hash {
        print_result(&hash);
    }     
}


fn print_help(){
    println!("SM3 Hash Help:");
    println!("USAGE:");
    println!("\tMain [FLAGS] [ARGS]");
    println!("");
    println!("FLAGS:");
    println!("\t-f, --file\tHash file");
    println!("\t-i, --input\tHash input");
    println!("");
    println!("ARGS:");
    println!("\tfile path or input string");
}


fn print_result(hash:&[u32]){
    println!("--== SM3 Hash Result ==--");
    print!("HEX:");
    for h in hash {
        print!("{:X}",h);
    }
    println!("\n--=====================--");
}

